package com.bid;



import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.animator.AlphaMenuAnimator;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Display;
/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
 *
 * @author Nicolas Gramlich
 * @since 18:47:08 - 19.03.2010
 */
public class BID extends BaseGameActivity implements IOnMenuItemClickListener {
        // ===========================================================
        // Constants
        // ===========================================================

        private static final float CAMERA_WIDTH_DEFAULT = 800;
        private static final float CAMERA_HEIGHT_DEFAULT = 480;
        
        private static final int MENU_NEW_GAME = 0;
        private static final int MENU_OPTIONS = 1;
        private static final int MENU_QUIT = 2;
        
        // ===========================================================
        // Fields
        // ===========================================================
        
        private MenuScene mMenuScene;
        private BitmapTextureAtlas mBitmapTextureAtlas;
        
        private Camera mCamera;
        
        public static float CAMERA_WIDTH;
        public static float CAMERA_HEIGHT;
        private static float ratioWidth, ratioHeight;
        public static float ratioMedium;
        public static boolean soundOn;
        
        private BitmapTextureAtlas mMenuTextureAtlas;	/*Texture*/
        private BitmapTextureAtlas mBackgroundTextureAtlas; /*Texture*/
        private TiledTextureRegion mBackgroundTextureRegion; /*Background*/
        private TiledTextureRegion mMenuNewGametTextureRegion;	/*Menu NewGame*/
        private TiledTextureRegion mMenuQuitTextureRegion;	/*Menu Quit*/
        private TiledTextureRegion mSoundTextureRegion; /*Sound*/

        // ===========================================================
        // Constructors
        // ===========================================================

        // ===========================================================
        // Getter & Setter
        // ===========================================================

        // ===========================================================
        // Methods for/from SuperClass/Interfaces
        // ===========================================================

        @Override
        public Engine onLoadEngine() {
        	
        	/*Calculate display settings. */
        	final Display display = getWindowManager().getDefaultDisplay();
        	CAMERA_WIDTH = display.getWidth();
        	CAMERA_HEIGHT = display.getHeight();
        	ratioWidth = (float)CAMERA_WIDTH / CAMERA_WIDTH_DEFAULT;
        	ratioHeight = (float)CAMERA_HEIGHT / CAMERA_HEIGHT_DEFAULT;
        	ratioMedium = Math.min(ratioWidth, ratioHeight);
        	
        	/*Load engine. */
            mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
            
            final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
            engineOptions.getTouchOptions().setRunOnUpdateThread(true);
            return new Engine(engineOptions);
        }

        @Override
        public void onLoadResources() {
        	this.mMenuTextureAtlas = new BitmapTextureAtlas(256, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        	this.mBackgroundTextureAtlas = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        	
            BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
            
            this.mMenuNewGametTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mMenuTextureAtlas, this, "main_menu_new_game.png", 0, 0, 1, 1);//256x64*2
            this.mMenuQuitTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mMenuTextureAtlas, this, "main_menu_quit.png", 0, 128, 1, 1);//256x64*2

            this.mSoundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mMenuTextureAtlas, this, "menu_sound.png", 0, 256, 1, 2);//64x64*2
            
            this.mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBackgroundTextureAtlas, this, "main_background_tiled.png", 0, 0, 1, 2);//1024x512*2
            
            this.mEngine.getTextureManager().loadTexture(this.mMenuTextureAtlas);
            this.mEngine.getTextureManager().loadTexture(this.mBackgroundTextureAtlas);
        }

        @Override
        public Scene onLoadScene() {
                this.mEngine.registerUpdateHandler(new FPSLogger());
                
                this.mMenuScene = new MenuScene(this.mCamera);
        		this.mMenuScene.setMenuAnimator(new AlphaMenuAnimator(20f));
        		
        		this.mMenuScene.attachChild(new CustomBackground(0, 0, BID.CAMERA_WIDTH, BID.CAMERA_HEIGHT, this.mBackgroundTextureRegion));
        		
        		final MenuItem newGameMenuItem = new MenuItem(MENU_NEW_GAME, this.mMenuNewGametTextureRegion);
        		newGameMenuItem.setSize(BID.CAMERA_WIDTH / 2, BID.CAMERA_HEIGHT / 4f);
        		newGameMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        		this.mMenuScene.addMenuItem(newGameMenuItem);
        		
        		final MenuItem quitMenuItem = new MenuItem(MENU_QUIT, this.mMenuQuitTextureRegion);
        		quitMenuItem.setSize(BID.CAMERA_WIDTH / 2, BID.CAMERA_HEIGHT / 4f);
        		quitMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        		this.mMenuScene.addMenuItem(quitMenuItem);
        		
        		this.mMenuScene.buildAnimations();
        		
        		final SharedPreferences settings = getSharedPreferences("settings", MODE_PRIVATE);		
                final SharedPreferences.Editor editor = settings.edit();
                BID.soundOn = settings.getBoolean("sound_on", true);
        		editor.putBoolean("sound_on", BID.soundOn);
        		editor.commit();
        		
        		AnimatedSprite soundMenu = new AnimatedSprite(CAMERA_WIDTH - 80 * ratioMedium, 20 * ratioMedium, 60 * ratioMedium, 60 * ratioMedium, this.mSoundTextureRegion){
        			@Override
        			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY){
        				if(pSceneTouchEvent.isActionDown()){
        					BID.soundOn = !BID.soundOn;
        					if(BID.soundOn)
        						this.setCurrentTileIndex(0);
        					else
        						this.setCurrentTileIndex(1);
        				}
        				editor.putBoolean("sound_on", BID.soundOn);
        				editor.commit();
        				return true;
        			}
        		};
        		if(BID.soundOn)
					soundMenu.setCurrentTileIndex(0);
				else
					soundMenu.setCurrentTileIndex(1);
        		
        		this.mMenuScene.attachChild(soundMenu);
        		this.mMenuScene.registerTouchArea(soundMenu);
        		this.mMenuScene.setOnMenuItemClickListener(this);
     
                return this.mMenuScene;
        }

        @Override
        public void onLoadComplete() {

        }

        @Override
        public void onResumeGame() {
                super.onResumeGame();
        }

        @Override
        public void onPauseGame() {
                super.onPauseGame();
        }
        
		@Override
		public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY) {
			switch(pMenuItem.getID()) {
			case MENU_NEW_GAME:
				this.startLevelPicker();
				return true;
			case MENU_OPTIONS:
				
				return true;
			case MENU_QUIT:
				/* End Activity */
				this.finish();
				return true;
			default:
				return false;
			}
		}

		

        // ===========================================================
        // Methods
        // ===========================================================
		private void startLevelPicker(){
        	Intent intent = new Intent(BID.this,LevelPicker.class);
			startActivity(intent);
		}
		
        // ===========================================================
        // Inner and Anonymous Classes
        // ===========================================================
		
		
}